package github._3y.algorithms;

import java.util.Arrays;

/**
 * @Author: DRevelation
 * @Date: 2021/1/20 19:53
 */
public class QuickSort {

    public static void main(String[] args) {
        int[] arr = new int[]{3, 1, 5, 2, 4};
        quickSort(arr, 0, arr.length - 1);
        System.out.println("=======================");
        System.out.println("arr = " + Arrays.toString(arr));
    }

    private static void quickSort(int[] arr, int L, int R) {
        int i = L;
        int j = R;
        int pivot = arr[(L + R) / 2];
        while (i <= j) {
            while (pivot > arr[i]) {
                i++;
            }
            while (pivot < arr[j]) {
                j--;
            }
            if (i <= j) {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                i++;
                j--;
            }
            System.out.println(Arrays.toString(arr));
        }
        if (L < j) {
            quickSort(arr, L, j);
        }
        if (i < R) {
            quickSort(arr, i, R);
        }
    }

}
